package com.wsz.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsz.pojo.Bot;
import com.wsz.pojo.R;
import com.wsz.pojo.dto.BotAddDto;
import com.wsz.pojo.dto.BotUpdateDto;
import com.wsz.user.service.BotService;
import com.wsz.utils.SecurityContextUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("/bot")
public class BotController {

    private final BotService botService;

    @GetMapping("/getlist/{limit}/{page}")
    public R getBotList(@PathVariable Long limit,
                    @PathVariable Long page){
        Page<Bot> page1 = new Page<>(page,limit);

        QueryWrapper<Bot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",SecurityContextUtils.getUserId());

        botService.page(page1, queryWrapper);

        List<Bot> records = page1.getRecords();
        long total = page1.getTotal();

        return R.ok().data("bots",records).data("total",total);
    }

    @GetMapping("/getlist")
    public R getBotList(){
        List<Bot> list = botService.list(new QueryWrapper<Bot>().eq("user_id",SecurityContextUtils.getUserId()));
        return R.ok().data("bots",list);
    }

    @PostMapping("/add")
    public R addBot(@RequestBody BotAddDto bot){
        return botService.addBot(bot);
    }

    @DeleteMapping("/remove")
    public R removeBot(@RequestParam String id){

        Bot byId = botService.getById(id);

        if(!byId.getUserId().equals(SecurityContextUtils.getUserId())){
            return R.error().message("没有权限");
        }

        boolean b = botService.removeById(id);
        if(b) return R.ok();
        return R.error().message("删除bot失败");
    }

    @PutMapping("/update")
    public R updateBot(@RequestBody BotUpdateDto bot) {
        return botService.updateBot(bot);
    }

}
